﻿using System;
using System.Collections.Generic;
using System.Linq;
using Foothill.Common;
using Foothill.DataAccess;
using Foothill.DataAccess.Repository;
using Foothill.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Foothill.Filter.Test
{
    [TestClass]
    public class GetCategoryStatsTest 
    {
        // this is the class under test
        private AdSearchRepo _adSearchRepo;

        public GetCategoryStatsTest()
        {
            // construct a AdSearchRepo and uses the test database
            _adSearchRepo = new AdSearchRepo();
            AdSearchRepo.UseDatabase(DatabaseToUse.Test);
        }

        private int GetCount(int idValue, IList<StatsItem> list)
        {
            StatsItem statsItem = list.FirstOrDefault(p => p.GroupId == idValue);
            return statsItem != null ? statsItem.Count : 0;
        }

        [TestMethod]
        public void ShouldReturnOneForUsedCarCategory()
        {
            //DatabaseUtility.ClearAdForSaleTable();
            //DatabaseUtility.ClearPhotoTable();

            //// insert a used car into an empty database
            //ForSaleAd vancar = RepositoryUtility.InsertOnePostIntoRepositoryWithPhoto(ModelId.EL, MakeId.Acura, VehicleCondition.Used, 2002, 14000, TransmissionType.Automatic, 4500, ListingTypes.Owner, City.Vancouver, "778 558 7771", "byOwner@gmail.com", DateTime.Now);

            //ForSaleAdAdvancedSearchParameter searchParam = TestUtility.ConstructSimpleSearchParameter(MakeId.Acura, ModelId.EL,
            //    2002, 2003, 0, GlobalValues.DropdownMaxValue, 0, GlobalValues.DropdownMaxValue, VehicleCondition.Both,
            //    ListingTypes.Both, 1, 2, OrderByItems.ByLastEditTime, fhSortDirection.DESC);

            ////IList<StatsItem> result = _adSearchRepo.GetCategoryStats(AdListCategory.Condition, searchParam);

            //// should have 1 in the used car category
            //int count = GetCount((int)VehicleCondition.Used, result);
            //int newCarCount = GetCount((int)VehicleCondition.New, result);
            //Assert.AreEqual(1, count);
            //Assert.AreEqual(0, newCarCount);
        }

        [TestMethod]
        public void CompositeTest()
        {
            //DatabaseUtility.ClearAdForSaleTable();
            //DatabaseUtility.ClearPhotoTable();

            //ForSaleAd car1 = RepositoryUtility.InsertOnePostIntoRepositoryWithPhoto(ModelId.EL, MakeId.Acura, VehicleCondition.New, 2002, 14000, TransmissionType.Manual, 4500, ListingTypes.Owner, City.Vancouver, "778 558 7771", "byOwner@gmail.com", DateTime.Now);
            //ForSaleAd car2 = RepositoryUtility.InsertOnePostIntoRepositoryWithPhoto(ModelId.EL, MakeId.Acura, VehicleCondition.New, 2002, 14000, TransmissionType.Manual, 4500, ListingTypes.Dealer, City.Vancouver, "778 558 7771", "byOwner@gmail.com", DateTime.Now);

            //ForSaleAdAdvancedSearchParameter searchParam = TestUtility.ConstructSimpleSearchParameter(MakeId.Acura, ModelId.EL,
            //    2002, 2003, 0, GlobalValues.DropdownMaxValue, 0, GlobalValues.DropdownMaxValue, VehicleCondition.Both,
            //    ListingTypes.Both, TransmissionType.Both, 1, 2, OrderByItems.ByLastEditTime, fhSortDirection.DESC);

            ////IList<StatsItem> result = _adSearchRepo.GetCategoryStats(AdListCategory.Condition, searchParam);

            //int count1 = GetCount((int)VehicleCondition.Used, result);
            //int count2 = GetCount((int)VehicleCondition.New, result);

            //// expecting 0 used car, 2 new cars
            //Assert.AreEqual(0, count1);
            //Assert.AreEqual(2, count2);

            ////result = _adSearchRepo.GetCategoryStats(AdListCategory.ListingType, searchParam);
            //count1 = GetCount((int)ListingTypes.Dealer, result);
            //count2 = GetCount((int)ListingTypes.Owner, result);

            //// expecting 1 by dealer and 1 by owner
            //Assert.AreEqual(1, count1);
            //Assert.AreEqual(1, count2);

            ////result = _adSearchRepo.GetCategoryStats(AdListCategory.TransmissionType, searchParam);
            //count1 = GetCount((int)TransmissionType.Manual, result);
            //count2 = GetCount((int)TransmissionType.Automatic, result);
            
            //// expecting 1 for manual and 1 for auto
            //Assert.AreEqual(2, count1);
            //Assert.AreEqual(0, count2);
        }
    }
}
